import { _decorator, Component, Node, systemEvent, SystemEvent, CameraComponent, PhysicsRayResult, geometry, PhysicsSystem, Vec3, AudioSourceComponent, Vec2 } from "cc";
import { EventMgr } from "../Managers/EventMgr";
import { SoundMgr } from "../Managers/SoundMgr";
import { EnemyCtrl } from "./EnemyCtrl";
const { ccclass, property } = _decorator;
//特价9.9元一套cocoscreator代码联系Q2483367084 
//截图https://share.weiyun.com/leGAHpnB 密码:b9udtv
@ccclass("ShootCtrl")
export class ShootCtrl extends Component {

    private camera: CameraComponent = null;
    private ray: geometry.ray = geometry.ray.create();

    private cursor3D: Node = null;

    private isShooting: boolean = false; 
    private shootTime: number = 0.3; // 配置表,  
    private nowTime: number = 0;

    private screenPos: Vec2 = cc.v2(0, 0); // 屏幕坐标位置;

    private hp: number = 100;   // 
    private config: any = null;

    public initPlayer(config): void {
        this.config = config;
        this.hp = config.hp;

        EventMgr.Instance.dispatch_event("UIEvent_BloodChange", 1); 
    }

    onLoad(): void {
        this.cursor3D = this.node.getChildByName("cursor");
        systemEvent.on(SystemEvent.EventType.TOUCH_START, this.onTouchStart, this);
        systemEvent.on(SystemEvent.EventType.TOUCH_END, this.onTouchEnd, this);
        EventMgr.Instance.add_event_listenner("UICursorMove", this, this.onUICursorMove);
        EventMgr.Instance.add_event_listenner("AttackPlayer", this, this.onAttackPlayer);
    }

    private onAttackPlayer(uname, killVlaue): void {
        this.hp -= killVlaue;
        console.log("hp=", this.hp);
        this.hp = (this.hp < 0) ? 0 : this.hp;
        // 通知UI，更新玩家的血量;
        EventMgr.Instance.dispatch_event("UIEvent_BloodChange", this.hp / this.config.hp); 
        // end 

        if (this.hp <= 0) { // 游戏失败;
           EventMgr.Instance.dispatch_event("PlayerDead", null);
        }
    }

    private onTouchStart(e): void {
        if (this.node.active === false) {
            return;
        }

        this.isShooting = true;
        this.nowTime = this.shootTime;
    }

    private onTouchEnd(e): void {
        this.isShooting = false;
    }

    public bindCamera(camera: CameraComponent): void {
        this.camera = camera;
    }


    private onUICursorMove(uname, udata): void {
        var screenPos: Vec3 = udata;
        this.screenPos.x = screenPos.x;
        this.screenPos.y = screenPos.y;

        this.camera.screenPointToRay(screenPos.x, screenPos.y, this.ray);

        if (PhysicsSystem.instance.raycastClosest(this.ray)) {
            var rlt: PhysicsRayResult = PhysicsSystem.instance.raycastClosestResult;
            var point = rlt.hitPoint; // 射击点;
            this.node.lookAt(point);

            this.cursor3D.setWorldPosition(point);
        }
    }

    onDestroy(): void {
        EventMgr.Instance.remove_event_listenner("AttackPlayer", this, this.onAttackPlayer);
        EventMgr.Instance.remove_event_listenner("UICursorMove", this, this.onUICursorMove);
    }

    doShoot(): void {
        // 播放开枪的声音, AudioSourceComponent--->playOneShot
        SoundMgr.Instance.playOneShot("sounds/M60_fire"); // 配置表;
        // end 

        // 射击目标;
        this.camera.screenPointToRay(this.screenPos.x, this.screenPos.y, this.ray);

        if (PhysicsSystem.instance.raycastClosest(this.ray)) {
            var rlt: PhysicsRayResult = PhysicsSystem.instance.raycastClosestResult;
            if (rlt.collider.node.name === "enemy") { // 射击到了是敌人;
                var ctrl = rlt.collider.node.getComponent(EnemyCtrl);
                ctrl.onAttacked(this.config.killVlaue); // 可以从表格里面根据玩家的等级，来获取;
            }

            var point = rlt.hitPoint; // 射击点;
            this.node.lookAt(point);
            this.cursor3D.setWorldPosition(point);
        }
        // end 
    }

    shootUpdate(dt: number): void {
        this.nowTime += dt;
        if (this.nowTime >= this.shootTime) {
            this.nowTime = 0;
            this.doShoot();
        }
    }

    update(dt: number): void {
        if(this.isShooting === true) {
            this.shootUpdate(dt);
        }
    }
}
